package com.beisita.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class ClickHouseController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private Redisson redisson;

    @RequestMapping("/deduct_stock")
    public String deductStock() {
        int stock;
        synchronized (this) {
            String nosqlStock = stringRedisTemplate.opsForValue().get("stock");
            stock = StringUtils.isNotBlank(nosqlStock) ? Integer.parseInt(nosqlStock) : 0;
            if (stock > 0) {
                int realStock = stock - 1;
                stringRedisTemplate.opsForValue().set("stock", realStock + "");
                System.out.println("扣减成功，剩余库存：" + realStock);
            } else {
                System.out.println("扣减失败，库存不足。");
            }
        }
        return "end";
    }

    @RequestMapping("/deduct_stock2")
    public String deductStock2() {
        String lockKey = "prod001";
        String PROD_NAME = "prodName";
        try {
//            boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, PROD_NAME);
            // 防止死锁
//            stringRedisTemplate.expire(lockKey, 10, TimeUnit.SECONDS);
            // 已存在不处理 返回false 不存在则设置值 同时加上过期时间 存在执行时间超过10S 然后自动清除 导致锁失效的问题
            boolean result = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, PROD_NAME, 10, TimeUnit.SECONDS);
            if (!result) {
                return "请等待";
            }
            int stock;
            String nosqlStock = stringRedisTemplate.opsForValue().get("stock");
            stock = StringUtils.isNotBlank(nosqlStock) ? Integer.parseInt(nosqlStock) : 0;
            if (stock > 0) {
                int realStock = stock - 1;
                stringRedisTemplate.opsForValue().set("stock", realStock + "");
                System.out.println("扣减成功，剩余库存：" + realStock);
            } else {
                System.out.println("扣减失败，库存不足。");
            }

        } catch (Exception e) {

        } finally {
            stringRedisTemplate.delete(lockKey);
        }

        return "end";
    }

    @RequestMapping("/deduct_stock3")
    public String deductStock3() {
        String lockKey = "prod001";
        String PROD_NAME = "prodName";
        RLock redissonLock = redisson.getLock(lockKey);
        try {
            redissonLock.lock();
            int stock;
            String nosqlStock = stringRedisTemplate.opsForValue().get("stock");
            stock = StringUtils.isNotBlank(nosqlStock) ? Integer.parseInt(nosqlStock) : 0;
            if (stock > 0) {
                int realStock = stock - 1;
                stringRedisTemplate.opsForValue().set("stock", realStock + "");
                System.out.println("扣减成功，剩余库存：" + realStock);
            } else {
                System.out.println("扣减失败，库存不足。");
            }

        } catch (Exception e) {

        } finally {
            redissonLock.unlock();
        }

        return "end";
    }

}
